package com.shell.customer.base.rx;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.customer.base.CusEvent;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.base.vo.CusRecVo;
import com.shell.customer.po.CusRec;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.governor.organization.po.PubOrgan;
import com.shell.governor.user.po.PubUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UpdateCusForEditSubscriber extends OnErrorNotImplementedSubscriber<CusEvent> {
	@Autowired
	private CusRecService customerCService;

	public void onApplicationEvent(CusEvent event) {
		CusRecVo cusVo = event.getCusRecVo();
		PubOrgan organ = event.getOrgan();
		PubUser user = event.getUser();

		CusRec cus = new CusRec();
		BeanUtils.copyProperties(cusVo, cus);

		cus.setContactorId(user.getUserId());
		if (StringUtil.emptyAndNull(cus.getCusId())) {
			cus.setCusState("E");
			if (StringUtil.notEmpty(organ.getOrganId())) {
				cus.setCusId(organ.getOrganId());
			} else {
				cus.setCusId(SystemUtil.getUUID());
			}
			cusVo.setCusId(cus.getCusId());
			cusVo.setCusState(cus.getCusState());
			this.customerCService.save(cus);
		} else {
			this.customerCService.updateNotNull(cus);
		}
	}
}
